package com.quyunshuo.module.home.activity.splash.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.times
import com.quyunshuo.module.home.R
import kotlinx.coroutines.delay

@Composable
fun SplashPage(goMain:()->Unit={}) {
    var showProgress by remember { mutableStateOf(true) }
    Box(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .padding(top = 100.dp)
                .align(Alignment.TopCenter),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(R.drawable.setting_icon), contentDescription = null,
                modifier = Modifier
                    .size(100.dp)
            )
            Spacer(Modifier.height(20.dp))
            Text("Edwin Goal111", color = colorResource(R.color.white), fontSize = 20.sp)
        }
        Column(
            modifier = Modifier
                .padding(bottom = 20.dp)
                .align(Alignment.BottomCenter),
        ) {
            if (showProgress) {
                ProgressScreen(onComplete = {
                    showProgress = false
                    // 这里添加实际跳转逻辑
                    // 例如: navController.navigate("nextScreen")
                    println("进度完成，跳转到下一个界面")
                })
            } else {
                goMain.invoke()
            }
        }

    }
}

@Composable
fun ProgressScreen(onComplete: () -> Unit) {
    var progress by remember { mutableStateOf(0f) }
    val totalTime = 2_000L // 10秒
    val updateInterval = 50L // 更新间隔(毫秒)

    // 自动更新进度
    LaunchedEffect(Unit) {
        val steps = (totalTime / updateInterval).toInt()
        val increment = 1f / steps

        repeat(steps) {
            delay(updateInterval)
            progress += increment
        }
        onComplete() // 完成后跳转
    }

    Row {

        // 自定义进度条
        Box(
            modifier = Modifier
                .padding(start = 20.dp)
                .width(280.dp)
                .height(12.dp)
                .background(Color.Gray, RoundedCornerShape(12.dp))
        ) {
            Box(
                modifier = Modifier
                    .height(12.dp)
                    .width(progress.coerceIn(0f, 1f) * 280.dp)
                    .background(Color.White, RoundedCornerShape(12.dp))
            )
        }
        Spacer(modifier = Modifier.weight(1f))
        // 显示进度文本
        Text(
            text = "进度: ${(progress * 100).toInt()}%",
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            color = Color.White,
            modifier = Modifier.padding(end = 20.dp)
        )

    }
}


@Preview
@Composable
fun SplashPagePreView() {
    SplashPage()
}